/**
 * export-util
 */
 import Vue from 'vue'
 /**
  * 导出检测是否请求错误，无错误信息则导出，有则提示
  * @param res
  * @param fileName
  */
 export function downLoadFileReader(res, fileName) {
   let data = res.data ? res.data : res
   let fileReader = new FileReader()
   fileReader.onload = function() {
     // new Promise((resolve, reject) => {
       try {
         let jsonData = JSON.parse(this.result) // 说明是普通对象数据，后台转换失败
         console.log(jsonData)
         if (!jsonData.success) {
           Vue.prototype.$Jmessage.error(jsonData.message)
           // reject(jsonData)
         }
       } catch (err) {
         // 解析成对象失败，说明是正常的文件流
         downLoadMix(res, fileName)
         // resolve(res)
       }
     // })
   }
   fileReader.readAsText(data)
 }
/**
 * 导出流文件
 * @param res
 * @param fileName
 */
export function downLoadMix (res, fileName) {
  let blob = new Blob([res]); //res为从后台返回的数据
  if (!fileName) {
    fileName = res.headers["content-disposition"].split("filename=").pop();
  }
  if ("msSaveOrOpenBlob" in navigator) {
    window.navigator.msSaveOrOpenBlob(blob, fileName);
  } else {
    const elink = document.createElement("a");
    elink.download = fileName;
    elink.style.display = "none";
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href);
    document.body.removeChild(elink);
  }
}
